<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Spider_upd{
	public $version;
	
	function __construct(){
		$this->EE=& get_instance();
		$this->version='0.0';
		
	}
	
	//module installer
	function install(){
            $this->EE->load->dbforge();
            
            $data=array(
                'module_name'=>'Spider',
                'module_version'=>  $this->version,
                'has_cp_backend'=>'y',
                'has_publish_fields'=>'n'
                
                );
            $this->EE->db->insert('modules', $data);
            
            /*
             * creating spider module tables
             */
            $this->EE->dbforge->add_field(array(
                'campaign_id'=>array('type' => 'int', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE, 'null'=>false),
                'campaign_name'=>array('type'=>'varchar', 'constraint'=>45, 'null'=>false, 'default'=>'Untitled'),
                'no_of_entry'=>array('type' => 'int', 'constraint' => 6, 'unsigned' => TRUE, 'null'=>false, 'default'=>2),
                'keyword'=>array('type'=>'text'),
                'channel_id'=>array('type'=>'int', 'constraint' => 6, 'null'=>true),
                'api'=>array('type'=>'text'),
                'active'=>array('type'=>'int', 'constraint' => 1, 'null'=>false, 'default'=>1)
                
            ));
            
            $this->EE->dbforge->add_key('campaign_id', true);
            $this->EE->dbforge->create_table('spider_campaign', TRUE);
            $this->EE->db->query("ALTER TABLE ".$this->EE->db->dbprefix('spider_campaign')." ENGINE = InnoDB");
            
            //spider fields
            $this->EE->dbforge->add_field(array(
                'id'=>array('type' => 'int', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE, 'null'=>false),
                'campaign_id'=>array('type' => 'int', 'constraint' => 11, 'unsigned' => TRUE, 'null'=>false),
                'api_name'=>array('type'=>'varchar', 'constraint' => 45),
                'fields'=>array('type'=>'text')
                
            ));
            
            $this->EE->dbforge->add_key('id', true);
            $this->EE->dbforge->create_table('spider_fields', TRUE);
            $this->EE->db->query("ALTER TABLE ".$this->EE->db->dbprefix('spider_fields')." ENGINE = InnoDB");
            $this->EE->db->query("ALTER TABLE  ".$this->EE->db->dbprefix('spider_fields')." ADD CONSTRAINT ".$this->EE->db->dbprefix('spider_fields_ibfk_1')." FOREIGN KEY (`campaign_id`) REFERENCES ".$this->EE->db->dbprefix('spider_campaign')." (`campaign_id`) ON DELETE CASCADE ON UPDATE CASCADE");
            
            //twitter table
            $this->EE->dbforge->add_field(array(
                'id'=>array('type' => 'int', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE, 'null'=>false),
                'campaign_id'=>array('type' => 'int', 'constraint' => 11, 'unsigned' => TRUE, 'null'=>false),
                'geocode'=>array('type' => 'int', 'constraint' => 1, 'unsigned' => TRUE, 'null'=>false, 'default'=>0),
                'latituted'=>array('type' => 'float', 'constraint' => 11, 'null'=>false, 'default'=>0),
                'logituted'=>array('type' => 'float', 'constraint' => 11, 'null'=>false, 'default'=>0),
                'radius'=>array('type' => 'float', 'constraint' => 11, 'unsigned' => TRUE, 'null'=>false, 'default'=>0),
                'unit'=>array('type'=>'enum', 'constraint' => '\'mi\', \'km\'', 'default'=>'mi', 'null'=>false),
                'lang'=>array('type'=>'varchar', 'constraint'=>'5', 'null'=>false, 'default'=>'en')
            ));
            
            $this->EE->dbforge->add_key('id', true);
            $this->EE->dbforge->create_table('spider_twitter', TRUE);
            $this->EE->db->query("ALTER TABLE ".$this->EE->db->dbprefix('spider_twitter')." ENGINE = InnoDB");
            $this->EE->db->query("ALTER TABLE ".$this->EE->db->dbprefix('spider_twitter')." ADD CONSTRAINT ".$this->EE->db->dbprefix('spider_twitter_ibfk_1')." FOREIGN KEY (`campaign_id`) REFERENCES ".$this->EE->db->dbprefix('spider_campaign')." (`campaign_id`) ON DELETE CASCADE ON UPDATE CASCADE");
            
            //youtube table
            $this->EE->dbforge->add_field(array(
                'id'=>array('type' => 'int', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE, 'null'=>false),
                'campaign_id'=>array('type' => 'int', 'constraint' => 11, 'unsigned' => TRUE, 'null'=>false),
                'embedcode'=>array('type' => 'int', 'constraint' => 1, 'unsigned' => TRUE, 'null'=>false, 'default'=>0),
                'width'=>array('type' => 'int', 'constraint' => 4, 'unsigned' => TRUE, 'null'=>false, 'default'=>560),
                'height'=>array('type' => 'int', 'constraint' => 4, 'unsigned' => TRUE, 'null'=>false, 'default'=>349),
                'lang'=>array('type'=>'varchar', 'constraint'=>'5', 'null'=>false, 'default'=>'en')
            ));
            
            $this->EE->dbforge->add_key('id', true);
            $this->EE->dbforge->create_table('spider_youtube', TRUE);
            $this->EE->db->query("ALTER TABLE ".$this->EE->db->dbprefix('spider_youtube')." ENGINE = InnoDB");
            $this->EE->db->query("ALTER TABLE ".$this->EE->db->dbprefix('spider_youtube')." ADD CONSTRAINT ".$this->EE->db->dbprefix('spider_youtube_ibfk_1')." FOREIGN KEY (`campaign_id`) REFERENCES ".$this->EE->db->dbprefix('spider_campaign')." (`campaign_id`) ON DELETE CASCADE ON UPDATE CASCADE");
            
            //filckr table
            $this->EE->dbforge->add_field(array(
                'id'=>array('type' => 'int', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE, 'null'=>false),
                'campaign_id'=>array('type' => 'int', 'constraint' => 11, 'unsigned' => TRUE, 'null'=>false),
                'user_id'=>array('type'=>'TINYTEXT'),
                'tags'=>array('type'=>'TINYTEXT'),
                'image_size'=>array('type'=>'CHAR', 'constraint'=>'1', 'null'=>false, 'default'=>'m'),
                'title'=>array('type' => 'int', 'constraint' => 1, 'unsigned' => TRUE, 'null'=>false, 'default'=>0)
            ));
            
            $this->EE->dbforge->add_key('id', true);
            $this->EE->dbforge->create_table('spider_flickr', TRUE);
            $this->EE->db->query("ALTER TABLE ".$this->EE->db->dbprefix('spider_flickr')." ENGINE = InnoDB");
            $this->EE->db->query("ALTER TABLE ".$this->EE->db->dbprefix('spider_flickr')." ADD CONSTRAINT ".$this->EE->db->dbprefix('spider_flickr_ibfk_1')." FOREIGN KEY (`campaign_id`) REFERENCES ".$this->EE->db->dbprefix('spider_campaign')." (`campaign_id`) ON DELETE CASCADE ON UPDATE CASCADE");
            
            return TRUE;
	}
        
        //module uninstaller
	function uninstall(){
            
            $this->EE->load->dbforge();
				
            $this->EE->db->select('module_id');
            $query = $this->EE->db->get_where('modules', array('module_name' => 'Spider'));
            
            $this->EE->db->where('module_id', $query->row('module_id'));
            $this->EE->db->delete('module_member_groups');
            
            $this->EE->db->where('module_id', $query->row('module_id'));
            $this->EE->db->delete('modules');
            
            
            //deleting module tables
            $this->EE->dbforge->drop_table('spider_fields');
            $this->EE->dbforge->drop_table('spider_twitter');
            $this->EE->dbforge->drop_table('spider_youtube');
            $this->EE->dbforge->drop_table('spider_flickr');
            $this->EE->dbforge->drop_table('spider_campaign');
		
            return TRUE;
	}
	
}